package com.ruoyi.web.controller.anchor;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.anchor.domain.LiveCart;
import com.ruoyi.anchor.service.LiveCartService;
import com.ruoyi.base.domain.Store;
import com.ruoyi.base.service.StoreService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LiveCartController extends BaseController {

    @Autowired
    private LiveCartService liveCartService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private Snowflake snowflake;

    @PostMapping("/anchor/live-cart/create")
    public AjaxResult create(@RequestBody LiveCart cart) {

        LoginUser loginUser = SecurityUtils.getLoginUser();

        cart.setCartId(snowflake.nextIdStr());
        cart.setAnchorId(loginUser.getUserId());
        Store store = storeService.findStoreByCodeAndName(cart.getStoreCode(), cart.getProductName());
        cart.setStoreId(store.getStoreId());
        cart.setProductCode(store.getProductCode());
        cart.setRetailPrice(store.getRetailPrice());
        cart.setCreateTime(DateUtils.getNowDate());
        cart.setClickCount(0);
        cart.setIsUse("是");

        liveCartService.saveLiveCart(cart);

        return success();
    }


}
